<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/tcms_registry_keys.class.php');
require_once($WSC->get_site_inc_dir().'modules/registry_db/registry_key_db.class.php');


class TCMS_Settings_Cache_DBC extends Tangra_Class implements I_DB_Storable {
	private $id;
	private $switch;
	private $expiry;
	private $locked;

	//fake
	public function set_id($id) {
	}


	//fake
	public function get_id() {
		return 1;
	}


	public function save(DB_Connection $dbc) {
		$this->switch->save($dbc);
		$this->expiry->save($dbc);
		$this->locked->save($dbc);

		return 1;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);


		$switch_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_CACHE_SWITCH);
		if ($switch_id) {
			$switch = new Registry_Key_DB();
			$switch->load_by_id($dbc, $switch_id);
			$this->switch = $switch;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_CACHE_SWITCH);
		}

		$expiry_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_CACHE_EXPIRY);
		if ($expiry_id) {
			$expiry = new Registry_Key_DB();
			$expiry->load_by_id($dbc, $expiry_id);
			$this->expiry = $expiry;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_CACHE_EXPIRY);
		}


		// locked key was added in 1.0.4
		$locked_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_CACHE_LOCKED);
		$locked = new Registry_Key_DB();
		if ($locked_id) {
			$locked->load_by_id($dbc, $locked_id);
		} else {
			$locked->set_value(0);
		}
		$this->locked = $locked;

		return 1;
	}


	public function set_switch($value) {
		$this->switch->set_value($value ? 1 : 0);
	}


	public function get_switch() {
		return $this->switch->get_value();
	}


	public function set_expiry($value) {
		tangra_if_not_int_throw_e($value);
		$this->expiry->set_value($value);
	}


	public function get_expiry() {
		return $this->expiry->get_value();
	}


	public function set_locked($value) {
		$this->locked->set_value($value ? 1 : 0);
	}


	public function get_locked() {
		return $this->locked->get_value();
	}
}